<template>
  <el-dialog width="900px" top="5%" fullscreen title="上游商品更新记录" v-model="dialogFormVisible" :draggable="true"
    @close="dialogClose">
    <div class="search-box">
      <div>
        <span>上游渠道：</span>
        <el-select style="width:100%" v-model="searchForm.channelCode" filterable clearable @change="typeChange">
          <el-option v-for="item in channelOptions" :key="item.channelCode" :label="item.channelName" :value="item.channelCode" />
        </el-select>
      </div>
      <div>
        <span>上游商品:</span>
        <el-input v-model="searchForm.channelProductName" clearable placeholder="请输入上游商品名称" />
      </div>
      <el-button style="margin-left:0;" type="primary" @click="searchHandle">查询</el-button>
    </div>
    <div class="table-box">
      <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column prop="channelName" label="平台名称" align="center">
          <template v-slot="scope">
            {{ scope.row.channelName || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="channelProductName" label="上游商品" align="center" min-width="140">
          <template v-slot="scope">
            <div class="t-flex">
              <div>{{ scope.row.channelProductName }}</div>
              <div class="imp-text" style="flex-shrink: 0;" v-show="scope.row?.channelProductNameNew">更改为：</div>
              <div v-show="scope.row?.channelProductNameNew">{{ scope.row?.channelProductNameNew }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="channelProductInfo" label="商品内容" align="center" min-width="300">
          <template v-slot="scope">
            <div class="t-flex">
              <div>{{ scope.row.channelProductInfo }}</div>
              <div class="imp-text" style="flex-shrink: 0;" v-show="scope.row?.channelProductInfoNew">更改为：</div>
              <div v-show="scope.row?.channelProductInfoNew">{{ scope.row?.channelProductInfoNew }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="typeName" label="类型" align="center">
          <template v-slot="scope">
            <div class="t-flex">
              <div>{{ scope.row.typeName }}</div>
              <div class="imp-text" style="flex-shrink: 0;" v-show="scope.row?.typeNameNew">更改为：</div>
              <div v-show="scope.row?.typeNameNew">{{ scope.row?.typeNameNew }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="categoryName" label="分类" align="center">
          <template v-slot="scope">
            <div class="t-flex">
              <div>{{ scope.row.categoryName }}</div>
              <div class="imp-text" style="flex-shrink: 0;" v-show="scope.row?.categoryNameNew">更改为：</div>
              <div v-show="scope.row?.categoryNameNew">{{ scope.row?.categoryNameNew }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="brandName" label="品牌" align="center">
          <template v-slot="scope">
            <div class="t-flex">
              <div>{{ scope.row.brandName }}</div>
              <div class="imp-text" style="flex-shrink: 0;" v-show="scope.row?.brandNameNew">更改为：</div>
              <div v-show="scope.row?.brandNameNew">{{ scope.row?.brandNameNew }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="channelPrice" label="结算价格(元)" align="center" min-width="150">
          <template v-slot="scope">
            <div class="t-flex">
              <div>{{ numFormat(scope.row.channelPrice) || '' }}</div>
              <div class="imp-text" style="flex-shrink: 0;" v-show="scope.row?.channelPriceNew">更改为：</div>
              <div v-show="scope.row?.channelPriceNew">{{ numFormat(scope.row?.channelPriceNew) }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="更新时间" align="center" min-width="160">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="createUser" label="更新人" align="center">
          <template v-slot="scope">
            {{ scope.row.createUser }}
          </template>
        </el-table-column>
        <el-table-column prop="recordType" label="操作类别" align="center">
          <template v-slot="scope">
            {{ scope.row.recordType == 1 ? '管理员操作更新' : scope.row.recordType == 2 ? '自动调价' : '' }}
          </template>
        </el-table-column>
      </el-table>
      <div class="table-footer">
        <el-pagination style="margin-left: auto;" v-model:currentPage="page" v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关 闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { reactive, toRefs } from 'vue'
import api from '../api'
import { warningDeal, numFormat, getWindowWidthAndHeight, getDateTime } from '@/utils/utils'
export default {
  setup() {
    const state = reactive({
      loading: false,
      dialogFormVisible: false,

      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      searchForm: {},
      tableData: [],

      channelOptions: [],
    })

    function dialogShow(channelCode = null) {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 300
      if (channelCode) {
        state.searchForm.channelCode = channelCode
      }
      state.dialogFormVisible = true
      getTableData()
      getChannelList()
    }

    function dialogClose() {
      state.page = 1
      state.tableData = []
      state.searchForm = {}
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        orderBy: 'id desc',
      }
      api.productModifyRecord(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data?.list
          state.total = res.data?.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function getChannelList() {
      const obj = {
        page: 1,
        size: 1000,
      }
      api.channelList(obj).then(res => {
        if (res.code == '200') {
          state.channelOptions = res.data?.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    return {
      ...toRefs(state),
      dialogShow,
      dialogClose,
      numFormat,
      handleSizeChange,
      handleCurrentChange,
      getDateTime,
      searchHandle,
    }
  }
}
</script>

<style lang="scss" scoped>
.table-footer {
  margin-top: 20px;
  display: flex;
  align-items: center;
}
.search-box {
  display: flex;
  align-items: center;
  gap: 20px;
  box-sizing: border-box;
  padding: 5px;
  margin-top: 10px;
  > div {
    display: flex;
    width: 300px;
    align-items: center;
    gap: 10px;
    > span {
      flex-shrink: 0;
      font-size: 14px;
    }
  }
}
.table-box {
  margin-top: 20px;
}
.t-flex {
  display: flex;
  flex-flow: column;
  .imp-text {
    color: #000000;
    font-weight: bold;
  }
}
</style>